﻿using System;
using System.Collections.Generic;
using Workbench.Models;
using Workbench.Operations;

namespace Workbench.Presentation
{
    public class ProjectViewModel : TabViewModelBase
    {
        private ProjectTreeViewModel projectTreeViewModel;
        public ProjectTreeViewModel ProjectTreeViewModel
        {
            get { return projectTreeViewModel; }
            set { projectTreeViewModel = value; RaisePropertyChanged(); }
        }

        private POSTypesTreeViewModel posTypesTreeViewModel;
        public POSTypesTreeViewModel POSTypesTreeViewModel
        {
            get { return posTypesTreeViewModel; }
            set { posTypesTreeViewModel = value; RaisePropertyChanged(); }
        }

        #region Statistics

        private long paragraphs;
        public long Paragraphs
        {
            get { return paragraphs; }
            set { paragraphs = value; RaisePropertyChanged(); }
        }

        private long sentences;
        public long Sentences
        {
            get { return sentences; }
            set { sentences = value; RaisePropertyChanged(); }
        }

        private long textLengthInRunningWords;
        public long TextLengthInRunningWords
        {
            get { return textLengthInRunningWords; }
            set { textLengthInRunningWords = value; RaisePropertyChanged(); }
        }

        private long differentForms;
        public long DifferentForms
        {
            get { return differentForms; }
            set { differentForms = value; RaisePropertyChanged(); }
        }

        private long averageLengthOfParagraphInSentences;
        public long AverageLengthOfParagraphInSentences
        {
            get { return averageLengthOfParagraphInSentences; }
            set { averageLengthOfParagraphInSentences = value; RaisePropertyChanged(); }
        }

        private long averageLengthOfParagraphInWords;
        public long AverageLengthOfParagraphInWords
        {
            get { return averageLengthOfParagraphInWords; }
            set { averageLengthOfParagraphInWords = value; RaisePropertyChanged(); }
        }

        private long averageLengthOfSentenceInWords;
        public long AverageLengthOfSentenceInWords
        {
            get { return averageLengthOfSentenceInWords; }
            set { averageLengthOfSentenceInWords = value; RaisePropertyChanged(); }
        }

        private long maximumNumberOfWordsInSentence;
        public long MaximumNumberOfWordsInSentence
        {
            get { return maximumNumberOfWordsInSentence; }
            set { maximumNumberOfWordsInSentence = value; RaisePropertyChanged(); }
        }

        private long maximumNumberOfSentencesInParagraph;
        public long MaximumNumberOfSentencesInParagraph
        {
            get { return maximumNumberOfSentencesInParagraph; }
            set { maximumNumberOfSentencesInParagraph = value; RaisePropertyChanged(); }
        }

        #endregion Statistics

        public ProjectViewModel()
            : base("Project")
        {
            ProjectTreeViewModel = new ProjectTreeViewModel();
            ProjectTreeViewModel.ProjectChanged += ProjectTreeViewModel_ProjectChanged;
            POSTypesTreeViewModel = new POSTypesTreeViewModel();
        }

        void ProjectTreeViewModel_ProjectChanged(object sender, EventArgs args)
        {
            var rootDirectory = ProjectOperations.GetRootDirectory();
            var contextFiles = rootDirectory.GetContextIncludedFiles();
            POSTypesTreeViewModel.LoadPOSTypes(contextFiles);
            LoadStatistics(contextFiles);
        }
        
        public override void Unload()
        {
            ProjectTreeViewModel.Unload();
            POSTypesTreeViewModel.Unload();
            BindStatistics(QuantitativeMeasures.Initial);
        }

        public override void Load()
        {
            var rootDirectory = ProjectOperations.GetRootDirectory();
            var files = rootDirectory.GetFiles();
            var contextFiles = rootDirectory.GetContextIncludedFiles();
            var posTypes = ProjectOperations.GetPOSTypes();
            ProjectTreeViewModel.LoadDirectory(rootDirectory);
            POSTypesTreeViewModel.LoadPOSTypes(contextFiles);
            LoadStatistics(contextFiles);
        }

        public void LoadStatistics(IEnumerable<ProjectFileModel> contextFiles)
        {
            var qMeasures = ProjectOperations.GetQuantitativeMeasures(contextFiles);
            BindStatistics(qMeasures);
        }

        void BindStatistics(QuantitativeMeasures measures)
        {
            Paragraphs = measures.Paragraphs;
            Sentences = measures.Sentences;
            TextLengthInRunningWords = measures.TextLengthInRunningWords;
            DifferentForms = measures.DifferentForms;

            AverageLengthOfParagraphInSentences = (long)Math.Round(measures.AverageLengthOfParagraphInSentences);
            AverageLengthOfParagraphInWords = (long)Math.Round(measures.AverageLengthOfParagraphInWords);
            AverageLengthOfSentenceInWords = (long)Math.Round(measures.AverageLengthOfSentenceInWords);

            MaximumNumberOfSentencesInParagraph = measures.MaximumNumberOfSentencesInParagraph;
            MaximumNumberOfWordsInSentence = measures.MaximumNumberOfWordsInSentence;

        }
    }
}
